<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button
          type="primary"
          @click="handleForm({})"
          v-auth="'wms:wh:pos:whPosInvStockLock:edit'"
        >
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button> -->
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.invCode }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsWhPosWhPosInvStockLockList">
  import { unref, onMounted, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    whPosInvStockLockDelete,
    whPosInvStockLockListData,
  } from '/@/api/wms/wh/pos/whPosInvStockLock';
  import { companyTreeData } from '/@/api/sys/company';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { Table } from 'ant-design-vue';
  const userStore = useUserStore();

  const { t } = useI18n('wms.wh.pos.whPosInvStockLock');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'ant-design:book-outlined',
    value: meta.title || t('货位存量占用表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      // {
      //   label: t('公司'),
      //   field: 'companyCode',
      //   fieldLabel: 'company.companyName',
      //   component: 'TreeSelect',
      //   componentProps: {
      //     api: companyTreeData,
      //     allowClear: true,
      //     immediate: true,
      //     onChange: handleSuccess,
      //   },
      // },
      {
        label: t('产品编码'),
        field: 'basInv.viewCode',
        component: 'Input',
      },
      {
        label: t('产品名称'),
        field: 'basInv.invName',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'basWare.cwhname',
        component: 'Input',
      },
      {
        label: t('货位'),
        field: 'basPosition.posName',
        component: 'Input',
      },
      // {
      //   label: t('形象刊'),
      //   field: 'freeVO.cfree1',
      //   component: 'Input',
      // },
      {
        label: t('来源单据'),
        field: 'csource',
        component: 'Select',
        componentProps: {
          dictType: 'wh_stock_lock_source',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('单据号'),
        field: 'sourceId',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    // {
    //   title: t('公司'),
    //   dataIndex: 'company.companyName',
    //   key: 'a.company_code',
    //   sorter: true,
    //   width: 160,
    //   align: 'left',
    // },
    {
      title: t('产品编码'),
      dataIndex: 'basInv.viewCode',
      key: 'inv.view_code',
      sorter: true,
      width: 110,
      align: 'left',
      //slot: 'firstColumn',
    },
    {
      title: t('产品名称'),
      dataIndex: 'basInv.invName',
      key: 'inv.inv_name',
      sorter: true,
      width: 150,
      align: 'left',
      //slot: 'firstColumn',
    },
    {
      title: t('占用量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 70,
      align: 'right',
      showSummary: true,
    },
    {
      title: t('仓库'),
      dataIndex: 'basWare.cwhname',
      key: 'a.whcode',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('货位'),
      dataIndex: 'basPosition.posName',
      key: 'a.pos_code',
      sorter: true,
      width: 90,
      align: 'left',
    },
    // {
    //   title: t('形象刊'),
    //   dataIndex: 'freeVO.cfree1',
    //   key: 'a.cfree1',
    //   sorter: true,
    //   width: 100,
    //   align: 'left',
    // },
    {
      title: t('自由项2'),
      dataIndex: 'freeVO.cfree2',
      key: 'a.cfree2',
      sorter: true,
      width: 100,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('自由项3'),
      dataIndex: 'freeVO.cfree3',
      key: 'a.cfree3',
      sorter: true,
      width: 100,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('来源单据'),
      dataIndex: 'csource',
      key: 'a.csource',
      dictType: 'wh_stock_lock_source',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('单据号'),
      dataIndex: 'sourceId',
      key: 'a.source_id',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('锁定人'),
      dataIndex: 'createByName',
      key: 'a.create_by_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('锁定时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 110,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑货位存量占用表'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'wms:wh:pos:whPosInvStockLock:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除货位存量占用表'),
        popConfirm: {
          title: t('是否确认删除货位存量占用表'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'wms:wh:pos:whPosInvStockLock:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getDataSource }] = useTable({
    api: whPosInvStockLockListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    showCustomSummary: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await whPosInvStockLockDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  onMounted(async () => {
    handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      // companyCode: userStore.getProjecte.code,
    });
    reload();
    await getForm().setFieldsValue({
      // companyCode: userStore.getProjecte.code,
    });
  }

  const totalIqty = computed(() => {
    // 计算总报工数
    const tableData = getDataSource();
    console.log('tableData', tableData);
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.iqty || 0), 0);
    return sum;
  });
</script>
